package com.franklin.java.completion.method;

import com.franklin.java.completion.AbstractJavaDataCompletionContributor;
import com.franklin.java.completion.CompletionOrder;
import com.franklin.plugins.common.utils.DataCheckUtils;
import com.intellij.codeInsight.completion.CompletionParameters;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiParameter;
import com.intellij.psi.PsiType;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 添加方法注释到代码提示中
 * @author Franklin
 * @since 2021/9/6 14:52
 */
public class JavaMethodCommentCompletionContributor extends AbstractJavaDataCompletionContributor {
    @Override
    public List<LookupElement> getLookupElements(CompletionParameters parameters) {
        return null;
    }

    @Override
    public CompletionOrder getOrder() {
        return CompletionOrder.HIGH;
    }

    /**
     * 中间注释<br>
     * @author Franklin
     * @since 2021/9/6 15:30
     * @param psiMethod:
     * @param comment:
     * @return java.lang.String
     */
    protected String resolveMethodLookupElementTailText(PsiMethod psiMethod,String comment){
        String tailText = null;
        PsiParameter[] psiParameters = psiMethod.getParameterList().getParameters();
        String paramString = Arrays.stream(psiParameters)
            .map(psiParameter -> psiParameter.getType().getPresentableText() + " " + psiParameter.getName())
            .collect(Collectors.joining(","));
        tailText = "(" + paramString + ")  " + comment;
        return tailText;
    }

    /**
     * 方法返回值类型<br>
     * @author Franklin
     * @since 2021/9/6 15:30
     * @param psiMethod:
     * @param fieldType:
     * @return java.lang.String
     */
    protected String resolveMethodLookupElementType(PsiMethod psiMethod,String fieldType){
        return Optional.ofNullable(psiMethod.getReturnType())
            .map(PsiType::getPresentableText)
            .orElse(fieldType);
    }
}
